import java.io.*;

class Student implements Externalizable {
    String name;
    int age;

    public Student() {
        name = null;
        age = 0;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void show() {
        System.out.printf("姓名: %s, 年龄: %d\n", name, age);
    }

    public String toString() {
        return name + "@" + age;
    }

    public void readExternal(ObjectInput in) throws IOException {
        name = in.readUTF();
        age = in.readInt();
    }

    public void writeExternal(ObjectOutput os) throws IOException {
        os.writeUTF(name);
        os.writeInt(age);
    }
}

public class ObjectStreamTest {
    static public void rafTest(String [] args) throws Exception {
        RandomAccessFile fp = new RandomAccessFile("./file.test", "rw");
        if (args.length > 0) {
            Student s = new Student(args[0], Integer.parseInt(args[1]));
            fp.writeUTF(s.name);
            fp.writeInt(s.age);
        } else {
            Student s = new Student();
            s.name = fp.readUTF();
            s.age = fp.readInt();
            s.show();
        }
        fp.close();
    }

    static public void main(String [] args) throws Exception {
        // rafTest(args);
        if (args.length > 0) {
            Student s = new Student(args[0], Integer.parseInt(args[1]));
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("./file.test"));
            os.writeObject(s);
            os.close();
        } else {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream("./file.test"));
            Student s = (Student)is.readObject();
            s.show();
            is.close();
        }
    }
}
